package com.mj.processController;

import org.activiti.bpmn.BpmnAutoLayout;
import org.activiti.bpmn.model.*;
import org.activiti.bpmn.model.Process;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;

/**
 * @author cRyann
 * @create 2017-08-22 17:06
 * 使用实例：BpmnModel model = dynamicProcess.createDynamicPocess("m822","m822",list1);
 **/
@Controller
public class DynamicProcess {

    @Autowired
    RepositoryService repositoryService;
    @Autowired
    ProcessEngineCore processEngineCore;

    public BpmnModel createDynamicProcess(String processKey, String name, List<Map<String, String>> list) {
        BpmnModel model = new BpmnModel();
        Process process = new Process();
        model.addProcess(process);
        process.setId(processKey);

        process.addFlowElement(createStartEvent());
        process.addFlowElement(createEndEvent());
        for (int i = 0; i < list.size(); i++) {
            Map<String, String> map = list.get(i);
            process.addFlowElement(createUserTask(map.get("id"), map.get("name")));
            if (i == 0) {
                process.addFlowElement(createSequenceFlow("start", map.get("id")));
            } else if (i == list.size() - 1) {
                process.addFlowElement(createSequenceFlow(list.get(i - 1).get("id"), map.get("id")));
                process.addFlowElement(createSequenceFlow(map.get("id"), "end"));
            } else {
                process.addFlowElement(createSequenceFlow(list.get(i - 1).get("id"), map.get("id")));
            }
        }

        new BpmnAutoLayout(model).execute();

        Deployment deployment = repositoryService.createDeployment()
                .addBpmnModel(name + ".bpmn", model)
                .name(name)
                .deploy();
        processEngineCore.startInstanceByKey(deployment.getName(), null);
//        processEngineCore.pauseByDeploymentKey(deployment.getName());
        return model;
    }

    public BpmnModel createDynamicPocess(String processKey, String name,
                                         List<Map<String, String>> list,
                                         String id,  //task的ID
                                         String taskName //task的名称
    ) {
        BpmnModel model = new BpmnModel();
        Process process = new Process();
        model.addProcess(process);
        process.setId(processKey);

        process.addFlowElement(createStartEvent());
        process.addFlowElement(createEndEvent());
        for (int i = 0; i < list.size(); i++) {
            Map<String, String> map = list.get(i);
            process.addFlowElement(createUserTask(map.get(id), map.get(taskName)));
            if (i == 0) {
                process.addFlowElement(createSequenceFlow("start", map.get(id)));
            } else if (i == list.size() - 1) {
                process.addFlowElement(createSequenceFlow(list.get(i - 1).get(id), map.get(id)));
                process.addFlowElement(createSequenceFlow(map.get(id), "end"));
            } else {
                process.addFlowElement(createSequenceFlow(list.get(i - 1).get(id), map.get(id)));
            }
        }

        new BpmnAutoLayout(model).execute();

        Deployment deployment = repositoryService.createDeployment()
                .addBpmnModel(name + ".bpmn", model)
                .name(name)
                .deploy();
        return model;
    }

    protected UserTask createUserTask(String id, String name) {
        UserTask userTask = new UserTask();
        userTask.setName(name);
        userTask.setId(id);
        return userTask;
    }

    protected SequenceFlow createSequenceFlow(String from, String to) {
        SequenceFlow flow = new SequenceFlow();
        flow.setSourceRef(from);
        flow.setTargetRef(to);
        return flow;
    }

    protected StartEvent createStartEvent() {
        StartEvent startEvent = new StartEvent();
        startEvent.setId("start");
        return startEvent;
    }

    protected EndEvent createEndEvent() {
        EndEvent endEvent = new EndEvent();
        endEvent.setId("end");
        return endEvent;
    }
}
